package com.hzit.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class MySqlSessionFactory {

	/**
	 * 1. 得到SqlSessionFactory对象
	 * @return
	 * @throws IOException
	 */
	private static SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	/**
	 * 2. 得到一个SqlSession（相当于jdbc中的Connection）
	 * @param autoCommit
	 * @return
	 * @throws IOException
	 */
	public static SqlSession getSqlSessoin(boolean autoCommit) throws IOException {
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		return sqlSessionFactory.openSession(autoCommit);
	}
}
